<template>
  <div class="province-sale-box">
    <div class="select-box">
      <div class="province-box">
        <div class="select1">
          <AreaSelect></AreaSelect>
        </div>
        <DatePicker></DatePicker>
      </div>
    </div>
    <SumSale></SumSale>
    <TabDatas></TabDatas>
  </div>
</template>

<script>
import { Bus } from "../../Bus";
import { getCopartnerdatas, queryprovincialdatas } from "../../api/datas";
import AreaSelect from "../../components/sales/areaSelect";
import DatePicker from "../../components/sales/DatePicker";
import SumSale from "../../components/sales/SumSale";
import TabDatas from "../../components/sales/TabDatas";
export default {
  components: {
    AreaSelect,
    DatePicker,
    SumSale,
    TabDatas,
  },
  data() {
    return {
      areaArr: {
        province: "",
        city: "",
      },
      isShow: true,
      lists: [
        {
          name: "省",
          prop: "province",
        },
        {
          name: "市",
          prop: "city",
        },
        {
          name: "销量",
          prop: "sale",
        },
      ],
      cityDatas: [],
      role: 0,
      timeobj: {
        stime: "",
        etime: "",
      },
    };
  },
  mounted() {
    Bus.$emit("isshowcity", this.isShow);
    Bus.$emit("tab-lists", this.lists);
    Bus.$emit("tab-datas", this.cityDatas);
    Bus.$on("areadatas", (value) => {
      this.areaArr.province = value.province;
      this.areaArr.city = value.city;
    });
    Bus.$on("role", (value) => {
      this.role = value;
    });
    Bus.$on("timeobj", (value) => {
      this.timeobj = value;
    });
    this.getAllDatas();
  },
  methods: {
    //获取全部大客户合伙人数据
    getAllDatas() {
      getCopartnerdatas({}).then((res) => {
        let tab_Datas = {};
        if (this.role == 1) {
          tab_Datas = res.partners;
        } else {
          tab_Datas = res.counts;
        }
        Bus.$emit("tab-datas", tab_Datas);
      });
    },
    //查询大客户，合伙人数据
    querydatas() {
      let datas = {};
      let create_time = {};
      datas.province = this.areaArr.province;
      create_time.startTime = this.timeobj.stime;
      create_time.endTime = this.timeobj.etime;
      queryprovincialdatas({ datas }, { create_time }).then((res) => {
        let tab_Datas = {};
        if (this.role == 1) {
          tab_Datas = res.partners;
          if (this.areaArr.city != "" && this.areaArr.city != "全部市") {
            tab_Datas = tab_Datas.filter((item) => {
              if (this.areaArr.city == item.city) {
                return item;
              }
            });
          }else{
            tab_Datas = tab_Datas;
          }
        } else {
          tab_Datas = res.counts;
          if (this.areaArr.city != "" && this.areaArr.city != "全部市") {
            tab_Datas = tab_Datas.filter((item) => {
              if (this.areaArr.city == item.city) {
                return item;
              }
            });
          }else{
            tab_Datas = tab_Datas;
          }
        }
        Bus.$emit("tab-datas", tab_Datas);
      });
    },
  },
  watch: {
    role() {
      if (this.areaArr.province == "") {
        this.getAllDatas();
      } else {
        this.querydatas();
      }
    },
     areaArr:{
        deep:true,
        handler(){
          this.querydatas();
        }
     },
    timeobj() {
      this.querydatas();
    },
  },
};
</script>

<style lang="less" scoped>
.province-sale-box {
  padding-top: 30px;
}
.select-box {
  margin-left: 30px;
  width: inherit;
}
.province-box {
  display: flex;
}
.select1 {
  margin-right: 50px;
  width: 400px;
}
</style>